﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

namespace Zhuang.Utility.Common.Strings
{
    //// <summary>
    /// 字符串操作辅助类
    /// </summary>
    public class StringHelper
    {

        #region 一些基本的符号常量

        /**/
        /// <summary>
        /// 点符号 .
        /// </summary>
        public const string Dot = ".";

        /**/
        /// <summary>
        /// 下划线 _
        /// </summary>
        public const string UnderScore = "_";

        /**/
        /// <summary>
        /// 逗号加空格 ,
        /// </summary>
        public const string CommaSpace = ", ";

        /**/
        /// <summary>
        /// 逗号 ,
        /// </summary>
        public const string Comma = ",";

        /**/
        /// <summary>
        /// 左括号 (
        /// </summary>
        public const string OpenParen = "(";

        /**/
        /// <summary>
        /// 右括号 )
        /// </summary>
        public const string ClosedParen = ")";

        /**/
        /// <summary>
        /// 单引号 '
        /// </summary>
        public const string SingleQuote = "\'";

        /**/
        /// <summary>
        /// 斜线 \
        /// </summary>
        public const string Slash = @"\";

        #endregion

        private StringHelper()
        {
        }

        /**/
        /// <summary>
        /// 移除空格并首字母小写的Camel样式
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string ToCamel(string name)
        {
            string clone = name.TrimStart('_');
            clone = RemoveSpaces(ToProperCase(clone));
            return String.Format("{0}{1}", Char.ToLower(clone[0]), clone.Substring(1, clone.Length - 1));
        }

        /**/
        /// <summary>
        /// 移除空格并首字母大写的Pascal样式
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string ToCapit(string name)
        {
            string clone = name.TrimStart('_');
            return RemoveSpaces(ToProperCase(clone));
        }


        /**/
        /// <summary>
        /// 移除最后的字符
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string RemoveFinalChar(string s)
        {
            if (s.Length > 1)
            {
                s = s.Substring(0, s.Length - 1);
            }
            return s;
        }

        /**/
        /// <summary>
        /// 移除最后的逗号
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string RemoveFinalComma(string s)
        {
            if (s.Trim().Length > 0)
            {
                int c = s.LastIndexOf(",");
                if (c > 0)
                {
                    s = s.Substring(0, s.Length - (s.Length - c));
                }
            }
            return s;
        }

        /**/
        /// <summary>
        /// 移除字符中的空格
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string RemoveSpaces(string s)
        {
            s = s.Trim();
            s = s.Replace(" ", "");
            return s;
        }

        /**/
        /// <summary>
        /// 字符串首字母大写
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string ToProperCase(string s)
        {
            string revised = "";
            if (s.Length > 0)
            {
                if (s.IndexOf(" ") > 0)
                {
                    revised = Microsoft.VisualBasic.Strings.StrConv(s, Microsoft.VisualBasic.VbStrConv.ProperCase, 1033);
                }
                else
                {
                    string firstLetter = s.Substring(0, 1).ToUpper(new CultureInfo("en-US"));
                    revised = firstLetter + s.Substring(1, s.Length - 1);
                }
            }
            return revised;
        }

        /**/
        /// <summary>
        /// 判断字符是否NULL或者为空
        /// </summary>
        public static bool IsNullOrEmpty(string value)
        {
            if (value == null || value == string.Empty)
            {
                return true;
            }

            return false;
        }
    }

    /// <summary>
    /// 字符串操作辅助类测试代码
    /// </summary>
    public class TestStringHelper
    {
        public static string Execute()
        {
            string value = "test String,";

            string result = string.Empty;
            result += "使用StringHelper字符串操作辅助类：" + "\r\n";
            result += "原字符串为:" + value + "\r\n";
            result += "StringHelper.IsNullOrEmpty:" + StringHelper.IsNullOrEmpty(value) + "\r\n";
            result += "StringHelper.ToCamel:" + StringHelper.ToCamel(value) + "\r\n";
            result += "StringHelper.ToCapit:" + StringHelper.ToCapit(value) + "\r\n";
            result += "StringHelper.RemoveSpaces:" + StringHelper.RemoveSpaces(value) + "\r\n";
            result += "StringHelper.RemoveFinalChar:" + StringHelper.RemoveFinalChar(value) + "\r\n";
            result += "StringHelper.ToProperCase:" + StringHelper.ToProperCase(value) + "\r\n";

            result += "\r\n\r\n";
            return result;
        }
    }
}
